/*
 * Copyright (C) 2010-2014 Enrique Lara (enrique.posix2.lara@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package trs.wmata.api;

import android.content.Context;

import app.transittool.model.Stop;

import trs.wmata.TrainStation;
import static trs.wmata.api.ApiXml.CODE;
import static trs.wmata.api.ApiXml.LAT;
import static trs.wmata.api.ApiXml.LINE_CODE_1;
import static trs.wmata.api.ApiXml.LINE_CODE_2;
import static trs.wmata.api.ApiXml.LINE_CODE_3;
import static trs.wmata.api.ApiXml.LINE_CODE_4;
import static trs.wmata.api.ApiXml.LON;
import static trs.wmata.api.ApiXml.NAME;
import static trs.wmata.api.ApiXml.STATION;

import java.util.ArrayList;

import org.xml.sax.Attributes;

public class RailStations extends ApiResponse {
    private LiveTrainStation trainStation = null;
    private ArrayList<String> routeCodes = null;
    private final ArrayList<Stop> trainStations = new ArrayList<Stop>();
    private final Context context;

    public RailStations(final Context context) {
        this.context = context;
    }

    public ArrayList<Stop> getTrainStations() {
        return trainStations;
    }

    @Override
    public void startElement(final String lName, final Attributes atts) {
        if (STATION.equals(lName)) {
            trainStation = new LiveTrainStation();
            routeCodes = new ArrayList<String>(10);
        }
    }

    @Override
    public void endElement(final String lName, final StringBuilder buff) {
        if (STATION.equals(lName)) {
            if (trainStation != null) {
                trainStation.setRouteCodes(routeCodes.toArray(new String[routeCodes.size()]));
                trainStations.add(trainStation);
                trainStation = null;
            }
        } else if (trainStation != null) {
            final String str = buff.toString().trim();

            if (LAT.equals(lName)) {
                trainStation.setLat(toDouble(str, 0));
            } else if (LON.equals(lName)) {
                trainStation.setLon(toDouble(str, 0));
            } else if (NAME.equals(lName)) {
                trainStation.setName(str);
            } else if (CODE.equals(lName)) {
                trainStation.setCode(str);

                // } else if (StationTogether1.equals(lName)) {
                // trainStation.setAltCode(str);
            } else if (LINE_CODE_1.equals(lName) || LINE_CODE_2.equals(lName) || LINE_CODE_3.equals(lName) || LINE_CODE_4.equals(lName)) {
                routeCodes.add(str);
            }
        }
    }

    @Override
    public boolean doCaptureChars() {
        return (trainStation != null) || (routeCodes != null);
    }

    class LiveTrainStation implements Stop {
        private TrainStation staticTrainStation;
        private String code;
        private String name;
        private String[] routeCodes;
        private double lat;
        private double lon;

        public void setCode(final String code) {
            staticTrainStation = TrainStation.fromCode(code);
            this.code = code;
        }

        @Override
        public String getCode() {
            return code;
        }

        @Override
        public String getName() {
            return name;
        }

        @Override
        public double getLat() {
            return lat;
        }

        @Override
        public double getLon() {
            return lon;
        }

        @Override
        public String[] getRouteCodes() {
            return routeCodes;
        }

        @Override
        public String[] getRouteNames() {
            return routeCodes;
        }

        public void setName(final String name) {
            this.name = name;
        }

        public void setRouteCodes(final String[] routeCodes) {
            this.routeCodes = routeCodes;
        }

        public void setLat(final double lat) {
            this.lat = lat;
        }

        public void setLon(final double lon) {
            this.lon = lon;
        }

        @Override
        public boolean gotoView() {
            return staticTrainStation.newInstance(context).gotoView();
        }

        @Override
        public boolean gotoViewNearbyStops() {
            return staticTrainStation.newInstance(context).gotoViewNearbyStops();
        }
    }
}
